import Vue from "vue"

import VueRouter from "vue-router"

import store from "@/store"

// 路由懒加载
const Home = () => import("@pages/Home")
const Login = () => import("@pages/Login")
const Register = () => import("@pages/Register")
const Search = () => import("@pages/Search")
const Detail = () => import("@pages/Detail")
const AddCartSuccess = () => import("@pages/AddCartSuccess")
const ShopCart = () => import("@pages/ShopCart")
const Pay = () => import("@pages/Pay")
const PaySuccess = () => import("@pages/PaySuccess")
const Trade = () => import("@pages/Trade")
const Center = () => import("@pages/Center")
const MyOrder = () => import("@/pages/Center/MyOrder")
const TeamOrder = () => import("@/pages/Center/TeamOrder")

// 保存VueRouter的原型对象上的push方法
const lastPush = VueRouter.prototype.push
// 重写VueRouter的原型对象上的push方法
VueRouter.prototype.push = function (
  location,
  onComplete = () => { },
  onAbort = () => { }) {
  return lastPush.call(this, location, onComplete, onAbort)
}

Vue.use(VueRouter)

const router = new VueRouter({
  scrollBehavior() {
    return {
      x: 0,
      y: 0,
    };
  },
  mode: "history",
  routes: [
    {
      path: "/home",
      component: Home,
      name: "home"
    },
    {
      path: "/login",
      component: Login,
      name: "login",
      meta: { FooterIsHidden: true },
      // 路由独享守卫
      beforeEnter(to, from, next) {
        const token = store.state.user.token
        if (token) {
          next("/")
        } else {
          next()
        }
      }
    },
    {
      path: "/register",
      component: Register,
      name: "register",
      meta: { FooterIsHidden: true }
    },
    {
      path: "/detail/:skuId",
      component: Detail,
      name: "detail",
    },
    {
      path: "/search/:keyword?",
      component: Search,
      name: "search"
    },
    {
      path: "/addcartsuccess",
      component: AddCartSuccess,
      name: "addcartsuccess",
      // 路由独享守卫
      beforeEnter(to, from, next) {
        if (sessionStorage.getItem("cartData")) {
          next()
        } else {
          next("/shopcart")
        }
      }
    },
    {
      path: "/shopcart",
      component: ShopCart,
      name: "shopcart"
    },
    {
      path: "/pay",
      component: Pay,
      name: "pay",
      beforeEnter(to, from, next) {
        if (from.path.startsWith("/trade")) {
          next();
        } else {
          next("/shopcart");
        }
      },
    },
    {
      path: "/paysuccess",
      component: PaySuccess,
      name: "paysuccess",
      beforeEnter(to, from, next) {
        if (from.path.startsWith("/pay")) {
          next();
        } else {
          next("/shopcart");
        }
      },
    },
    {
      path: "/center",
      component: Center,
      children: [
        {
          path: "myorder",
          component: MyOrder,
          name: "myorder"
        },
        {
          path: "teamorder",
          component: TeamOrder,
          name: "teamorder"
        },
        {
          path: "",
          redirect: "/center/myorder"
        }
      ]
    },
    {
      path: "/trade",
      component: Trade,
      name: "trade",
      beforeEnter(to, from, next) {
        if (from.path.startsWith("/shopcart")) {
          next();
        } else {
          next("/shopcart");
        }
      },
    },
    {
      path: "/",
      redirect: "/home"
    },
  ]
})

// 全局前置守卫
router.beforeEach(async (to, from, next) => {

  //使用一个数组,保存所有必须有token才能访问的路由地址
  const checkPath = ["/center", "/pay", "/paysuccess", "/trade"];

  //得到当前to的地址是否在checkPath中
  const isCheckPath = checkPath.some((item) => {
    //判断to.path 是否以 item为开头(考虑多级路由的请求)
    return to.path.startsWith(item);
  });

  //获取vuex中的token和用户名
  const token = store.state.user.token;
  const nickName = store.state.user.nickName;
  //1
  if (nickName) {
    return next();
  }

  //2
  if (isCheckPath) {
    //当前路径需要token校验
    if (token) {
      try {
        //如果有token,则获取一下用户名,并放行
        await store.dispatch("user/getUsername");
        next();
      } catch (e) {
        next("/login");
      }
    } else {
      //如果没有token则直接去登录页登录
      next("/login");
    }
  } else {
    //当前路由不需要token校验(但是如果此时你是有token的,我们可以顺便获取一下用户名) //如果有token,则获取一下用户名,并放行
    if (token) {
      try {
        //如果有token,则获取一下用户名,并放行
        await store.dispatch("user/getUsername");
        next();
      } catch (e) {
        next("/login");
      }
    }

    next();
  }
});

export default router